<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-select v-model="searchTerms.status" placeholder="考试状态" class="select" clearable @change="getData">
            <el-option :value="1" label="未开始" />
            <el-option :value="2" label="考试中" />
            <el-option :value="3" label="已结束" />
          </el-select>
          <el-select v-model="searchTerms.examPaperType" placeholder="试卷分类" class="select" clearable @change="getData">
            <el-option v-for="item in examPaperTypeList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
          <el-date-picker
            v-model="searchTerms.date"
            :editable="false"
            class="picker"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm"
            @change="getData"/>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入考试名称" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div>
          <el-button v-if="queryMatch(158)" type="primary" icon="el-icon-plus" @click="toCreateExam">创建考试</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="name" label="考试名称" min-width="160" header-align="center" align="center" />
        <el-table-column label="试卷分类" width="140" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag type="info">{{ scope.row['type_name'] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="total_score" label="总分" min-width="120" header-align="center" align="center" />
        <el-table-column prop="subject_number" label="题目数" min-width="120" header-align="center" align="center" />
        <el-table-column prop="limit_time" label="限制时间" min-width="120" header-align="center" align="center" />
        <el-table-column prop="forObject" label="考试对象" min-width="260" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-for="item in scope.row['group_name']" :key="item" type="warning" style="margin: 2px;">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="start_time" label="考试时间" width="280" header-align="center" align="center">
          <template slot-scope="scope">
            {{ parseTime(scope.row['start_time'], '{y}-{m}-{d} {h}:{i}') + ' ~ '+ parseTime(scope.row['end_time'], '{y}-{m}-{d} {h}:{i}') }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="120" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-tag v-if="scope.row['status']===0" type="success">{{ scope.row['status_txt'] }}</el-tag>
            <el-tag v-else type="success">{{ scope.row['status_txt'] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-search" circle @click="toExamInfo(scope.row)"/>
            <el-tooltip v-if="queryMatch(188)" content="停止考试" placement="top">
              <el-button v-if="scope.row['status']===2" type="warning" size="small" circle icon="el-icon-minus"
                         @click="toStopExam(scope.row)"/>
            </el-tooltip>
            <el-button v-if="queryMatch(189) && scope.row['status']!==2" type="danger" size="small" circle icon="el-icon-delete"
                       @click="toDeleteExam(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      downloadLoading: false,
      searchTerms: {
        state: '',
        examPaperType: '',
        date: [],
        searchValue: ''
      },

      // 试卷类型
      examPaperTypeList: [],

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.getData();
    this.getExamPaperTypeList();
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'exam/examList'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.searchValue,
        status: this.searchTerms.status,
        typeId: this.searchTerms.examPaperType
      }
      this.searchTerms.date && this.searchTerms.date.length !== 0 && (data.startTime = this.searchTerms.date[0], data.endTime = this.searchTerms.date[1])
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        for (const i in this.tableData) {
          this.tableData[i]['group_name'] && (this.tableData[i]['group_name'] = this.tableData[i]['group_name'].split('/'));
        }
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // 获取试卷类型
    getExamPaperTypeList(){
      const url = 'exam/typeList';
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.examPaperTypeList = res.data;
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    toExamInfo(row) {
      this.$router.push({
        path: '/exam/examInfo',
        query: { id: row.id }
      })
    },

    toCreateExam() {
      this.$router.push({
        path: '/exam/create'
      })
    },

    toStopExam(row) {
      this.$confirm('此操作将停止本次考试, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'exam/stopTheExam/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg);
          this.getData();
        }).catch((err) => {
          this.$message.error(err.msg);
        })
      })
    },

    toDeleteExam(row) {
      this.$confirm('此操作将删除该场考试, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'exam/examDel/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg);
          this.getData();
        }).catch((err) => {
          this.$message.error(err.msg);
        })
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
  }
}
</style>

